package com.atguigu.springcloud.service.impl;  
  
import com.atguigu.springcloud.service.PaymentService;  
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;  
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;  
import org.springframework.stereotype.Service;  
import java.util.concurrent.TimeUnit;  
  
@Service  
public class PaymentServiceImpl implements PaymentService {  
  
    //成功：方法本身不抛异常也不超时，用这个方法测试方法外等待超时
    @HystrixCommand(
            // 请求处理失败之后，调用的备选方案方法
            fallbackMethod="paymentInfo_OK_FallBack",

            // 设置详细的参数
            commandProperties = {
                    // execution.isolation.thread.timeoutInMilliseconds 表示设置 Hystrix 的超时时间
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
            }
    )
    public String paymentInfo_OK(Integer id){  
        return "线程池："+Thread.currentThread().getName()+"   paymentInfo_OK,id：  "+id+"\t"+"哈哈哈"  ;  
    }

    public String paymentInfo_OK_FallBack(Integer id){
        System.out.println("OK 方法触发熔断机制，执行备选方案。线程名：" + Thread.currentThread().getName());
        return "抱歉！系统忙！id=" + id + " 线程名：" + Thread.currentThread().getName();
    }
  
    //失败
    // Hystrix 相关配置注解
//    @HystrixCommand(
//            // 请求处理失败之后，调用的备选方案方法
//            fallbackMethod="paymentInfo_TimeOut_FallBack",
//
//            // 设置详细的参数
//            commandProperties = {
//                    // execution.isolation.thread.timeoutInMilliseconds 表示设置 Hystrix 的超时时间
//                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
//            }
//    )
    public String payment_Timeout(Integer id){
        // 触发降级条件一：方法内超时
        int timeNumber = 6;
        try { TimeUnit.SECONDS.sleep(timeNumber); }catch (Exception e) {e.printStackTrace();}
        return "线程池："+Thread.currentThread().getName()+"   paymentInfo_TimeOut,id：  "+id+"\t"+"呜呜呜"+" 耗时(秒)"+timeNumber;

        // 触发降级条件二：方法内抛异常
//        System.out.println("触发降级条件二：方法内抛异常");
//        throw new RuntimeException("抛出运行时异常");
    }


    /**
     * 给 paymentInfo_TimeOut() 业务方法准备的备选方案
     * @param id
     * @return
     */
    public String paymentInfo_TimeOut_FallBack(Integer id) {
        return "抱歉！系统故障，抛出了异常，请稍后再试！id = " + id + " 线程名：" + Thread.currentThread().getName();
    }

    @HystrixCommand(
            fallbackMethod = "paymentCircuitBreakerFallback",
            commandProperties = {
                    // 开启断路器
                    @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),

                    // 在限定时间内，发送够多少个请求才能打开断路器
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "1"),

                    // 断路器从全开到半开状态中间的时间长度
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "15000")
            }
    )
    public String paymentCircuitBreaker(Integer id) {

        if (id > 50) {
            throw new RuntimeException("[Provider] paymentCircuitBreaker() 我疯啦！");
        }

        return "[Provider] paymentCircuitBreaker() 正常。";
    }

    public String paymentCircuitBreakerFallback(Integer id) {
        return "[Provider] paymentCircuitBreaker() 降级方案。id = " + id;
    }
}